treaty <- read.csv("CYEstimateLatentHRtreaty04.csv")

test <- treaty[,4:(ncol(treaty)-2)]
NA.test <- !is.na(test)

TREATY <- apply(test,1,sum,na.rm=T)
TOTAL <- apply(NA.test,1,sum,na.rm=T)

FULL <- TRUE
treaty$TREATY <- TREATY / TOTAL

treaty <- subset(treaty, select=c(ccode, year, TREATY, latentmean, latentsd))
names(treaty) <- c("COW", "YEAR","TREATY", "latentmean", "latentsd")

data <- subset(data, YEAR>=1976)

test <- NA
for(i in 1:1000){
    test[i] <- cor(treaty$TREATY, rnorm(rep(1,nrow(treaty)), mean=treaty$latentmean, sd=treaty$latentsd))
}
quantile(test, c(0.025, 0.5, 0.975))

COLOR <- colorRampPalette(blues9[-(1:3)])
COLOR <- colorRampPalette(c(grey(.8),grey(.6),grey(.2)))

par(mar=c(6,6,1,1))
plot(treaty$latentmean, treaty$TREATY, xlim=c(-6,6), pch=3, cex=.7, col=densCols(treaty$latentmean, treaty$additive, colramp=COLOR), xaxt="n", yaxt="n", xlab="", ylab="")
if(FULL==TRUE)axis(side=2, at=c(0,.2,.4,.6,.8,1), las=2, font=1)
if(FULL==FALSE)axis(side=2, at=c(0,.2,.4,.6,.8,1), las=2, font=1)
axis(side=1, at=-6:6, labels=-6:6, las=1, font=1)
if(FULL==TRUE)mtext(side=2, "Proportion of Total Number of Ratified Treaties", cex=1.7, line=3.5)
if(FULL==FALSE)mtext(side=2, "Proportion of Total Number of Ratified Treaties", cex=1.7, line=2.5)
mtext(side=1, "Latent Treaty Variable", cex=1.7, line=4)


